package com.ruoyi.company.service.impl;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.company.domain.XlkjCompanySurvey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.company.mapper.XlkjCompanyMapper;
import com.ruoyi.company.domain.XlkjCompany;
import com.ruoyi.company.service.IXlkjCompanyService;

/**
 * 单位Service业务层处理
 *
 * @author wang
 * @date 2022-01-11
 */
@Service
public class XlkjCompanyServiceImpl implements IXlkjCompanyService
{
    @Autowired
    private XlkjCompanyMapper xlkjCompanyMapper;

    /**
     * 查询单位
     *
     * @param id 单位主键
     * @return 单位
     */
    @Override
    public XlkjCompany selectXlkjCompanyById(Long id)
    {
        return xlkjCompanyMapper.selectXlkjCompanyById(id);
    }

    /**
     * 查询单位列表
     *
     * @param xlkjCompany 单位
     * @return 单位
     */
    @Override
    public List<XlkjCompany> selectXlkjCompanyList(XlkjCompany xlkjCompany)
    {
        return xlkjCompanyMapper.selectXlkjCompanyList(xlkjCompany);
    }

    /**
     * 新增单位
     *
     * @param xlkjCompany 单位
     * @return 结果
     */
    @Override
    public int insertXlkjCompany(XlkjCompany xlkjCompany)
    {
        xlkjCompany.setCreateTime(DateUtils.getNowDate());
        return xlkjCompanyMapper.insertXlkjCompany(xlkjCompany);
    }

    /**
     * 修改单位
     *
     * @param xlkjCompany 单位
     * @return 结果
     */
    @Override
    public int updateXlkjCompany(XlkjCompany xlkjCompany)
    {
        xlkjCompany.setUpdateTime(DateUtils.getNowDate());
        return xlkjCompanyMapper.updateXlkjCompany(xlkjCompany);
    }

    /**
     * 批量删除单位
     *
     * @param ids 需要删除的单位主键
     * @return 结果
     */
    @Override
    public int deleteXlkjCompanyByIds(Long[] ids)
    {
        return xlkjCompanyMapper.deleteXlkjCompanyByIds(ids);
    }

    /**
     * 删除单位信息
     *
     * @param id 单位主键
     * @return 结果
     */
    @Override
    public int deleteXlkjCompanyById(Long id)
    {
        return xlkjCompanyMapper.deleteXlkjCompanyById(id);
    }

    /**
     * 单位数据
     *
     * @param details
     */
    @Override
    public String importDevice(List<XlkjCompany> details) {
        if (StringUtils.isNull(details) || details.size() == 0){
            throw new CustomException("导入的单位数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (XlkjCompany user : details){
            try{
                this.insertXlkjCompany(user);
                successNum++;
                successMsg.append("导入成功");
            }catch (Exception e){
                failureNum++;
                String msg = "导入失败";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else{
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<XlkjCompany> selectCompanyByDepartmentId(String[] departmentId) {
        return xlkjCompanyMapper.selectCompanyByDepartmentId(departmentId);
    }

    @Override
    public XlkjCompanySurvey selectCompanyNumByDeptId(Long deptId) {
        return xlkjCompanyMapper.selectCompanyNumByDeptId(deptId);
    }
}
